package com.xwaf.platform.system.auth.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.constants.RedisKeys;
import com.xwaf.platform.common.redis.util.RedisUtil;
import com.xwaf.platform.common.service.service.CrudService;
import com.xwaf.platform.system.api.entity.auth.Dictionary;
import com.xwaf.platform.system.api.service.auth.DictionaryService;
import com.xwaf.platform.system.auth.dao.DictionaryDao;

/**
 * 
 * <p>
 * 字典
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019-5-30 16:02:40 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Transactional(readOnly = true)
@Service("dictionaryService")
public class DictionaryServiceImpl extends CrudService<DictionaryDao, Dictionary> implements DictionaryService {

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private DictionaryDao dictionaryDao;

	@Override
	public Page<Dictionary> selPageList(Page<Dictionary> page, Dictionary dictionary) {
		List<Dictionary> list = dictionaryDao.selPageList(page, dictionary);
		page.setRecords(list);
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dictionary> getRediesByKey(String key) {
		List<Dictionary> rList = new ArrayList<Dictionary>();
		if (key != null) {
			// 取缓存
			List<Dictionary> list = (List<Dictionary>) redisUtil.get(RedisKeys.SYS_DICT + key);
			if (list != null) {
				rList.addAll(list);
			} else { // 缓存没有，查数据库
				List<Dictionary> selectList = new ArrayList<Dictionary>();
				Wrapper<Dictionary> wrapper = new EntityWrapper<Dictionary>();
				wrapper.eq("config_key", key);
				wrapper.orderBy("value", true);
				selectList = dictionaryDao.selectList(wrapper);
				if (selectList != null) {
					rList.addAll(selectList);
					list = selectList;
					// 存缓存
					redisUtil.set(RedisKeys.SYS_DICT + key, list);
				}
			}
		}
		return rList;
	}

}
